package com.yanqu.road.dao.impl.player;

import com.yanqu.road.dao.db.DbNameStrategy;
import com.yanqu.road.dao.db.DbParameter;
import com.yanqu.road.dao.impl.TempDataObjectDao;
import com.yanqu.road.entity.activity.guagifts.UserGuaRelationData;
import com.yanqu.road.entity.qixi.UserQixiRelationData;

import java.sql.CallableStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Types;
import java.util.HashMap;
import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;

public class UserGuaRelationDaoImpl extends TempDataObjectDao<UserGuaRelationData> {
    public static String getUserGuaRelationDataSql = "select * from `t_u_gua_relation` where `activityId`=? and `userId`=?";

    public Map<Long, UserGuaRelationData> getUserGuaRelationData(int activityId, long userId) {
        ResultSet rs = null;
        CallableStatement call = null;
        Map<Long, UserGuaRelationData> dataMap = new HashMap<>();
        try {
            getDb().setConnName(DbNameStrategy.getUserDb());
            call = getDb().getConnection().prepareCall(getUserGuaRelationDataSql);
            int i = 1;
            call.setInt(i++, activityId);
            call.setLong(i++, userId);
            rs = call.executeQuery();
            while (rs.next()) {
                UserGuaRelationData data = parse(rs);
                dataMap.put(data.getTargetUserId(), data);
            }
        } catch (Exception e) {
            throw new RuntimeException(e);
        } finally {
            getDb().closeConnection(call, rs);
        }
        return dataMap;
    }

    public static String updateUserGuaRelationDataSql = "update `t_u_gua_relation` set getCount=?,sendCount=?,getTime=?,sendTime=? where activityId=? and userId=? and targetUserId=?";

    @Override
    protected boolean updateData(UserGuaRelationData userGuaRelationData) {
        userGuaRelationData.setBeginWriteTime();
        getDb().setConnName(DbNameStrategy.getUserDb());
        int i = 1;
        Map<Integer, DbParameter> para = new HashMap<Integer, DbParameter>();
        para.put(i++, new DbParameter(Types.INTEGER, userGuaRelationData.getGetCount()));
        para.put(i++, new DbParameter(Types.INTEGER, userGuaRelationData.getSendCount()));
        para.put(i++, new DbParameter(Types.BIGINT, userGuaRelationData.getGetTime()));
        para.put(i++, new DbParameter(Types.BIGINT, userGuaRelationData.getSendTime()));
        para.put(i++, new DbParameter(Types.INTEGER, userGuaRelationData.getActivityId()));
        para.put(i++, new DbParameter(Types.BIGINT, userGuaRelationData.getUserId()));
        para.put(i++, new DbParameter(Types.BIGINT, userGuaRelationData.getTargetUserId()));
        if (getDb().executeNoneQuery(updateUserGuaRelationDataSql, para) > 0) {
            return true;
        }
        return false;
    }

    public static String insertUserGuaRelationDataSql = "insert into `t_u_gua_relation`(activityId,userId,targetUserId,getCount,sendCount,getTime,sendTime) values (?,?,?,?,?,?,?)";

    @Override
    protected boolean addData(UserGuaRelationData userGuaRelationData) {
        userGuaRelationData.setBeginWriteTime();
        getDb().setConnName(DbNameStrategy.getUserDb());
        int i = 1;
        Map<Integer, DbParameter> para = new HashMap<Integer, DbParameter>();
        para.put(i++, new DbParameter(Types.INTEGER, userGuaRelationData.getActivityId()));
        para.put(i++, new DbParameter(Types.BIGINT, userGuaRelationData.getUserId()));
        para.put(i++, new DbParameter(Types.BIGINT, userGuaRelationData.getTargetUserId()));
        para.put(i++, new DbParameter(Types.INTEGER, userGuaRelationData.getGetCount()));
        para.put(i++, new DbParameter(Types.INTEGER, userGuaRelationData.getSendCount()));
        para.put(i++, new DbParameter(Types.BIGINT, userGuaRelationData.getGetTime()));
        para.put(i++, new DbParameter(Types.BIGINT, userGuaRelationData.getSendTime()));
        if (getDb().executeNoneQuery(insertUserGuaRelationDataSql, para) > 0) {
            return true;
        }
        return false;

    }

    @Override
    protected UserGuaRelationData parseData(ResultSet rs) throws SQLException {
        UserGuaRelationData userGuaRelationData = new UserGuaRelationData();
        userGuaRelationData.setActivityId(rs.getInt("activityId"));
        userGuaRelationData.setUserId(rs.getLong("userId"));
        userGuaRelationData.setTargetUserId(rs.getLong("targetUserId"));
        userGuaRelationData.setGetCount(rs.getInt("getCount"));
        userGuaRelationData.setSendCount(rs.getInt("sendCount"));
        userGuaRelationData.setGetTime(rs.getLong("getTime"));
        userGuaRelationData.setSendTime(rs.getLong("sendTime"));

        return userGuaRelationData;
    }
}
